package drawSplitScreenImage;

import screenCtrl.WindowStatus;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

public class DrawSplitScreenImageGraphics2D implements DrawSplitScreenImage{
    private static final DrawSplitScreenImageGraphics2D imageGraphics2D;
    static {
        imageGraphics2D = new DrawSplitScreenImageGraphics2D();
    }

    @Override
    public String drawImage(String path, List<WindowStatus> windowStatuses, WindowStatus deskWindowStatus) throws Exception {
        File file = new File(path);
        drawImage(file.getParent(),file.getName(),windowStatuses, deskWindowStatus);
        return null;
    }

    public static String drawImageStatic(String path, List<WindowStatus> windowStatuses,WindowStatus deskWindowStatus) throws Exception {
        return imageGraphics2D.drawImage(path,windowStatuses, deskWindowStatus);
    }

    @Override
    public String drawImage(String parent, String name, List<WindowStatus> windowStatuses, WindowStatus deskWindowStatus) throws Exception {
        File parentFile = new File(parent);
        if (!parentFile.exists()){
            throw new Exception("Error: 'parentPath:"+parentFile.getPath()+"' Not Found");
        }
        File fileImage = new File(parent + "/" + name);

        int width= Integer.parseInt(deskWindowStatus.getWidth());
        int height= Integer.parseInt(deskWindowStatus.getHeight());
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();
        ImageIO.write(drawWindowStatus(image,windowStatuses), "png", fileImage);
        return fileImage.getPath();
    }

    private static BufferedImage drawWindowStatus(BufferedImage image,List<WindowStatus> windowStatuses){
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(new Color(128, 128, 128, 255));
        int sp=20;//间隔大小
        for (WindowStatus windowStatus : windowStatuses) {
            int x= Integer.parseInt(windowStatus.getX()),y= Integer.parseInt(windowStatus.getY());
            if (x<0){
                x=0;
            }
            if (y<0){
                y=0;
            }
            int width= Integer.parseInt(windowStatus.getWidth()),height= Integer.parseInt(windowStatus.getHeight());
            if (width>=height){
                int y1 = (y+y+height)/2;
                int stroke = height-2*sp;
                int x1 = x+stroke/2+sp;
                int x2 = x+width-stroke/2-sp;
                g2d.setStroke(new BasicStroke(stroke));
                Line2D line = new Line2D.Float(x1, y1, x2, y1);
                g2d.draw(line);
            }else {
                int x1 = (x+x+width)/2;
                int stroke = width-2*sp;
                int y1 = y+stroke/2+sp;
                int y2 = y+height-stroke/2-sp;
                g2d.setStroke(new BasicStroke(stroke));
                Line2D line = new Line2D.Float(x1, y1, x1, y2);
                g2d.draw(line);
            }
        }
        g2d.dispose();
        return image;
    }

    public static String drawImageStatic(String parent, String name, List<WindowStatus> windowStatuses,WindowStatus deskWindowStatus) throws Exception {
        return imageGraphics2D.drawImage(parent,name,windowStatuses, deskWindowStatus);
    }
}
